package StuDemo.LinkedList;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: Dijah
 * @Date: 2022/12/20 16:36
 * @Desc: 全排列
 *  给定一个数组[1,2,3]
 *  123
 *  132
 *  213
 *  231
 *  312
 *  321
 */
public class code04_premute {


    static List<List<Integer>> res = new LinkedList<>();
    public static List<List<Integer>> premute(int [] nums){

        //记录路径
        LinkedList<Integer> strack = new LinkedList<>();
        backtrack(nums,strack);
        return res;
    }

    private static void backtrack(int[] nums, LinkedList<Integer> strack) {

        //触发结束条件
        if(strack.size() == nums.length){
            res.add(new LinkedList<>(strack));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            //排除不合法的
            if(strack.contains(nums[i])){
                continue;
            }
            //做选择
            strack.add(nums[i]);
            //进入下一层决策
            backtrack(nums,strack);
            //取消选择
            strack.removeLast();
        }
    }

    public static void main(String[] args) {
        int []nums = {1,2,3};
        List<List<Integer>> premute = premute(nums);
        for (List<Integer> stringList : premute) {
            System.out.println(stringList.toString());
        }


    }
}
